package com.sysu.simulation;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.gson.Gson;
import com.sysu.simulation.bean.Clothes;
import com.sysu.simulation.helper.AccountHelper;
import com.sysu.simulation.widget.AddClothesFragment;
import com.sysu.simulation.widget.ClothesListFragment;
import com.sysu.simulation.widget.EditClothesFragment;

import java.lang.ref.WeakReference;

public class ClothesActivity extends BaseActivity implements ActionMode.Callback,
        ClothesListFragment.OnClothesListClickListener,
        AddClothesFragment.OnClothesAddedListener,
        EditClothesFragment.OnClothesEditedListener{

    private static final String BUNDLE_USER_ID = "bundle_user_id";
    private ClothesListFragment clothesListFragment;
    private WeakReference<AddClothesFragment> addClothesFragment;
    private WeakReference<EditClothesFragment> editClothesFragment;
    private ActionMode actionMode;

    @Override
    protected void initWindow() {
        super.initWindow();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(false);
        }
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_USER_ID, AccountHelper.getUserId());
        clothesListFragment = ClothesListFragment.newInstance();
        clothesListFragment.setArguments(bundle);
        addFragment(R.id.clist_content, clothesListFragment);
    }

    @Override
    public boolean onSupportNavigateUp() {
        //finish();
        onBackPressed();
        return super.onSupportNavigateUp();
    }

    @Override
    public void onBackPressed() {
        if(mFragment instanceof ClothesListFragment || clothesListFragment==null) {
            super.onBackPressed();
            //finish();
        }
        else {
            addFragment(R.id.clist_content, clothesListFragment);
        }
    }

    public static void show(Context context) {
        context.startActivity(new Intent(context, ClothesActivity.class));
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_clothes;
    }

    public void startMultiMode() {
        if(actionMode == null)
            actionMode = startSupportActionMode(this);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        if (actionMode == null) {
            actionMode = mode;
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.multidelete_menu, menu);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_delete:
                clothesListFragment.deleteItems();
                actionMode.finish();
                return true;
            case R.id.menu_selectAll:
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        actionMode = null;
        clothesListFragment.setMultiMode(false);
    }

    @Override
    public void onFloatingButtonClick() {
        if(addClothesFragment==null || addClothesFragment.get()==null) {
            addClothesFragment = new WeakReference<>(AddClothesFragment.newInstance());
        }
        addFragment(R.id.clist_content, addClothesFragment.get());
    }

    @Override
    public void onClothesItemClick(Clothes item, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("clothes_item", new Gson().toJson(item));
        bundle.putInt("clothes_position", position);
        if(editClothesFragment==null || editClothesFragment.get()==null) {
            editClothesFragment = new WeakReference<>(EditClothesFragment.newInstance());
            editClothesFragment.get().setArguments(bundle);
        }
        else {
            editClothesFragment.get().setDefaultInput(bundle);
        }
        addFragment(R.id.clist_content, editClothesFragment.get());
    }

    @Override
    public void onClothesAdded(Clothes item) {
        if(clothesListFragment==null) finish();
        else {
            addAndRemoveFragment(R.id.clist_content, clothesListFragment);
            clothesListFragment.onClothesAdded(item);
        }
    }

    @Override
    public void onClothesEdited(Clothes item, int position) {
        if(clothesListFragment==null) finish();
        else {
            addAndRemoveFragment(R.id.clist_content, clothesListFragment);
            clothesListFragment.onClothesEdited(item, position);
        }
    }
}
